<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\Corp\Action\Dashboard\Traits;

use GuzzleHttp\Client;
use Hyperf\Guzzle\HandlerStackFactory;
use Hyperf\Utils\Codec\Json;

trait ValidateTokenTrait
{
    protected function validateToken(string $corpId, string $corpSecret)
    {
        $factory = new HandlerStackFactory();
        $stack = $factory->create();

        $client = make(Client::class, [
            'config' => [
                'base_uri' => 'https://qyapi.weixin.qq.com/',
                'handler' => $stack,
            ],
        ]);

        $response = $client->get('cgi-bin/gettoken', [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'verify' => false,
            'query' => [
                'corpid' => $corpId,
                'corpsecret' => $corpSecret,
            ],
        ]);

        return Json::decode($response->getBody()->getContents());
    }
}
